package com.UnionPayCenter.wechat;

import com.UnionPayCenter.constant.ConstantInterface;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.security.MessageDigest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author BaoJF
 * @Date: 2019/05/07
 */
@Slf4j
public class WeixinUtil {
	
	private final static String string = "0123456789";
    final private static char[] chars =  string.toCharArray();
	
	/**
     * 生成签名随机字符串
     * @return
     */
	public static String create_nonce_str() {
        String nonce = new String();
        for(int i=0; i<10; i++){
            int rannum = (int)(Math.random()*1000) % (chars.length);
            nonce += chars[rannum];
        }
        return nonce;
    }
    /**
     * 签名加密生成时间戳
     * @return
     */
    public static String create_timestamp() {
        return Long.toString(System.currentTimeMillis() / 1000);
    }

    /**
     * 生成签名sha1加密
     * @param str
     * @return
     */
    public static String getSha1(String str){
        if(str==null||str.length()==0){
            return null;
        }
        char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9',
                'a','b','c','d','e','f'};
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
            mdTemp.update(str.getBytes("UTF-8"));

            byte[] md = mdTemp.digest();
            int j = md.length;
            char buf[] = new char[j*2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(buf);
        } catch (Exception e) {
            return null;
        }
    }

    /**
    * @Description: 微信回调 Signature 检验
    * @Param:
    * @return:
    * @Author: bite
    * @Date: 2019/6/20
    */
    public static boolean checkSignature(String token, String signature,String timestamp,String nonce){
        String[] str = new String[]{token,timestamp,nonce};
        //排序
        Arrays.sort(str);
        //拼接字符串
        StringBuffer buffer = new StringBuffer();
        for(int i =0 ;i<str.length;i++){
            buffer.append(str[i]);
        }
        //进行sha1加密
        String temp = getSha1(buffer.toString());
        //与微信提供的signature进行匹对
        return signature.equals(temp);
    }


    public static Map<String,Object> xmltoMap(String xml) {
        try {
            Map<String,Object> map = new HashMap<String,Object>();
            Document document = DocumentHelper.parseText(xml);
            Element nodeElement = document.getRootElement();
            List<Element> node = nodeElement.elements();
            for (Element e : node) {
                map.put(e.getName(), e.getText());
            }
            node = null;
            nodeElement = null;
            document = null;
            return map;
        } catch (Exception e) {
            log.error("xmltoMap 解析失败", e);
        }
        return null;
    }
}
